<?php
/**
 * @title 113. 路径总和 II - 层次遍历
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param TreeNode $root
     * @param Integer $sum
     * @return Integer[][]
     */
    function pathSum($root, $sum) {
        if($root === null){
            return [];
        } else {}

        /**
         * @since 2020-09-27 结果集
         */
        $result = [];

        /**
         * @since 2020-09-27 层次遍历
         */
        $nexts = [
            [[], 0, $root],
        ];
        while($nexts){
            $nextTmp = [];

            /**
             * @since 2020-09-27 遍历节点
             */
            foreach ($nexts as list($nums, $numSum, $node)){
                $val = $node->val;
                $nums[] = $val;
                $nodeSum = $numSum+$val;

                /**
                 * @since 2020-09-27 叶子节点
                 */
                if($node->left === null AND $node->right === null){
                    if($nodeSum == $sum){
                        $result[] = $nums;
                    } else {}
                } else {
                    /**
                     * @since 2020-09-27 左右节点
                     * @since 2020-09-27 由于没有说节点都是正数, 即使 $nodeSum>$sum, 也不能剪枝
                     */
                    if($node->left !== null){
                        $nextTmp[] = [$nums, $nodeSum, $node->left];
                    } else {}
                    if($node->right !== null){
                        $nextTmp[] = [$nums, $nodeSum, $node->right];
                    } else {}
                }
            }

            /**
             * @since 2020-09-27 下次递归
             */
            $nexts = $nextTmp;
        }

        /**
         * @return
         */
        return $result;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202009/2020.09.26.php
 */
$datas = [
    [
        [5,4,8,11,null,13,4,7,2,null,null,5,1],
        22
    ],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    // $result = $obj->pathSum(...$data);
    $result = $obj->pathSum($xhprof->tree($data[0]), $data[1]);
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
